package com.meixiang.view.Calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * 作者：黑哥 on 2016/3/30 18:12
 *
 * 第一步实现周的显示
 *
 *
 */
public class WeekDay extends View {

    //定义星期一到星期日的显示颜色

    //星期一到星期天的的颜色
    private int mWeekColor = Color.parseColor("#555555");

    //定义星期的字体大小
    private int mWeekSize = 14;

    //定义星期
    private String[] weekString = new String[]{"日","一","二","三","四","五","六"};

    //初始化一个画笔
    private Paint paint;

    //
    private DisplayMetrics mDisplayMetrics;


    public WeekDay(Context context,AttributeSet attrs) {
        super(context);
        mDisplayMetrics = getResources().getDisplayMetrics();
        paint = new Paint();
    }
    /**
     *
     * 测量
     *
     * */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if(heightMode == MeasureSpec.AT_MOST){
            heightSize = mDisplayMetrics.densityDpi * 30;
        }
        if(widthMode == MeasureSpec.AT_MOST){
            widthSize = mDisplayMetrics.densityDpi * 300;
        }
        setMeasuredDimension(widthSize, heightSize);
    }

    /**
     *
     * 画笔
     *
     * 画要显示的控件
     * */
    @Override
    protected void onDraw(Canvas canvas) {


        /**
         * 画周日期
         *
         * */
        int width = getWidth();
        int height = getHeight();

        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(mWeekSize * mDisplayMetrics.scaledDensity);

        int columnWidth = width / 7;
        for(int i=0;i < weekString.length;i++){
            String text = weekString[i];
            int fontWidth = (int) paint.measureText(text);
            int startX = columnWidth * i + (columnWidth - fontWidth)/2;
            int startY = (int) (height/2 - (paint.ascent() + paint.descent())/2);
            paint.setColor(mWeekColor);
            canvas.drawText(text, startX, startY, paint);
        }

    }
    /**
     *
     * 设置星期一至星期日的颜色
     *
     * */
    public void semWeekColor(int mWeekColor){
        this.mWeekColor = mWeekColor;
    }

    /**
     * 设置字体的大小
     */
    public void setmWeekSize(int mWeekSize) {
        this.mWeekSize = mWeekSize;
    }


    /**
     *
     * 设置星期的形式
     *
     * 默认值	"日","一","二","三","四","五","六"
     * */

    public void  setWeekString(String[] weekString){
        this.weekString = weekString;
    }

}
